package net.net23.sysdent.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class DBSet {

	private ResultSet set;
	private Statement statement;

	public DBSet(PreparedStatement prepareStatement) throws SQLException {
		this.statement = prepareStatement;
		this.set = prepareStatement.executeQuery();
	}
	
	public DBSet(Statement prepareStatement, String sql) throws SQLException {
		this.statement = prepareStatement;
		prepareStatement.execute(sql);
		this.set = prepareStatement.getResultSet();
	}

	public boolean next() throws SQLException {
		boolean hasNext = set.next();
		if (!hasNext) {
			clean();
		}
		return hasNext;
	}

	public String getString(int index) throws SQLException {
		return set.getString(index);
	}

	public void clean() throws SQLException {
		statement.close();
		set.close();
		statement = null;
		set = null;
	}

	public int getInt(int index) throws SQLException {
		return set.getInt(index);
	}

	public ResultSetMetaData getMetadata() throws SQLException {
		return set.getMetaData();
	}

	public Object getObject(int index) throws SQLException {
		return set.getObject(index);
	}

}
